package cn.zhaopin.starter.security.convert;

import cn.zhaopin.starter.security.common.SecurityConstant;
import cn.zhaopin.starter.security.token.PhoneAndSmsAuthenticationToken;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.core.Authentication;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * Description: 手机号验证码授权信息转转换
 *
 * @author zuomin (myleszelic@outlook.com)
 * @date: 2021/10/29-13:58
 */
public class PhoneAndSmsAuthenticationConverter extends BasicAuthenticationConverter {

    @Override
    public Mono<Authentication> convert(ServerWebExchange exchange) {
        ServerHttpRequest request = exchange.getRequest();

        //设置路径缓存
        holderPathValue(request.getPath().value());

        //封装未认证的token
        String phoneNumber = obtainQueryFirst(request, SecurityConstant.PARAMETER.LOGIN_PHONE_NUM);
        String smsCode = obtainQueryFirst(request, SecurityConstant.PARAMETER.LOGIN_SMS_CODE);
        argsCheck(phoneNumber, smsCode);
        PhoneAndSmsAuthenticationToken authenticationToken = new PhoneAndSmsAuthenticationToken(phoneNumber, smsCode);
        return Mono.justOrEmpty(authenticationToken);
    }

}
